<template>
  <div class="t-gen-form">
    <div class="mb20">
      <h3
        v-if="formConfig.title"
        class="text-center form-name"
        v-html="formConfig.title"
      ></h3>
      <p
        v-if="formConfig.description"
        class="text-center"
        v-html="formConfig.description"
      ></p>
    </div>
    <Suspense>
      <template #default>
        <el-form
          ref="genFormRef"
          :model="pageFormModel"
          :rules="formRules"
          :scroll-to-error="true"
          :validate-on-rule-change="false"
          label-position="top"
          size="default"
          @submit.native.passive
        >
          <el-row style="margin: 0">
            <generate-form-item
              v-for="(item, index) in formConfig!.fields"
              :key="`${item.key}`"
              :index="index"
              :item="item"
              :seq-no="item.seqNo"
            />
          </el-row>
        </el-form>
      </template>
      <template #fallback>
        <async-loading />
      </template>
    </Suspense>
  </div>
</template>

<script lang="ts" name="PreviewForm" setup>
import { onBeforeMount, PropType, ref } from "vue";
import GenerateFormItem from "@/views/formgen/components/GenerateForm/GenerateFormItem.vue";
import type { FormInstance } from "element-plus";
import AsyncLoading from "@/components/TLoading/asyncLoading.vue";
import type { FormConfig } from "@/views/formgen/components/FormDesign/types/form";

const props = defineProps({
  // 表单配置
  formConfig: {
    type: Object as PropType<FormConfig>,
    required: false
  },
  // 表单数据对象
  pageFormModel: {
    type: Object,
    required: false
  }
});

console.log(props.formConfig);

const genFormRef = ref<FormInstance>();

const formRules = ref({});

// 创建阶段
onBeforeMount(() => {});

defineExpose({
  genFormRef
});
</script>
<style lang="scss" scoped>
.form-name {
  padding: 20px 30px 10px;
  border: 1px dashed transparent;
  line-height: 30px;
  margin: 0;
  display: var(--form-theme-showFormTitle, block);
  font-size: 24px;
  font-weight: bold;
}
</style>
